<template>
<!-- <form action="/api/users/login" method="post"> -->


    <SaveDialog ref="savedialogRef" :dialog-width="600" :title="dialogTitle"
        :column-list="LoginUserColumns" :saveType="saveType" :show-close="false" :show-size-set="false"
        submit-text="登 录" @afterSave="afterSave">
        <template #otherOpBtns>


<router-link to="/reg" style="text-decoration:none;margin:0 10px">
    <el-button size="small" type="primary" plain>我要注册</el-button>
</router-link>
</template>

    </SaveDialog>
<!-- </form> -->
</template>

<script setup>
import {ref,onMounted} from 'vue'
import { ElMessage,ElMessageBox } from 'element-plus'
import { login } from "../request/user"
import { useRouter, useRoute } from "vue-router";
import {LoginUserColumns,UserUrlSet,netUtil} from "../models/columns"
import { ClientStorage, SaveType,InitComponentData } from 'common-list'

InitComponentData(netUtil, UserUrlSet, 'id');//这里应该取消自动获取列表！！

const saveType = ref(SaveType.Check);
const router= useRouter()
const savedialogRef = ref();
const dialogObj = ref()
// 打开保存弹窗
const onOpenSaveDialog = () => {
    const row = {username:'',pwd:''}

    dialogTitle.value = '用户登录'

    dialogObj.value = row
    
    savedialogRef.value.openDialog(row);
};
let dialogTitle = ref('');
// const cannotSendCode = computed(() => {
//     return !dialogObj.value['userName'] || dialogObj.value['userName'].indexOf('@') == -1
// })
onMounted(async () => {
    // if (redirectUrl) {        
    //     regRoute.value += '?redirect=' + redirectUrl
    //     if (queryStr) {
    //         regRoute.value += '&params=' + queryStr          
    //     }
    // }

    onOpenSaveDialog()
})
const afterSave = async (loginResult) => {

//  console.log(SaveType.Check)

if (loginResult && loginResult.code == 200) {//登录成功
    const userInfo = loginResult.data
    
    ClientStorage.set('userInfo', userInfo, false, true) //设置用户信息到本地缓存中！ 是否安全，值得商
    localStorage.setItem('token','ok')
    localStorage.setItem('userid',userInfo.id)
    // let params = ''
    
    // if (queryStr) {
    //     params = JSON.parse(queryStr)
    // }
    // let query = params ? params : null
    let path ='/todotable'  // redirectUrl ? redirectUrl : 

    router.push(path)
} else {
    await ElMessageBox.alert('用户名密码错误！请重新输入！')
   // onOpenSaveDialog()
}

//获取最新添加的！

}

</script>